package com.note.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.note.api.UserService;
import com.note.common.ServerResponse;
import com.note.mapper.DeptMapper;
import com.note.mapper.UserMapper;
import com.note.mapper.UserRoleMapper;
import com.note.model.Dept;
import com.note.model.Tree;
import com.note.model.User;
import com.note.model.UserRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by ADMIN on 2018/3/22.
 */
@Service(version = "1.0.0")
public class UserServiceImpl implements UserService {


    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserRoleMapper userRoleMapper;

    @Autowired
    private DeptMapper deptMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public User get(Long id) {
        String userIdKey = "userId:" + id;
        String roleIdsKey = "roleIdsKey:" + id;

        ValueOperations<String, User> operations = redisTemplate.opsForValue();

        //缓存存在
        boolean hasUserIdKey = redisTemplate.hasKey(userIdKey);
        boolean hasRoleIdsKey = redisTemplate.hasKey(roleIdsKey);
        if (hasUserIdKey) {
            User user = operations.get(userIdKey);
        }
        if (hasRoleIdsKey) {
            List<Long> roleIds = (List<Long>) operations.get(roleIdsKey);
        }
        List<Long> roleIds = userRoleMapper.listRoleId(id);
        User user = userMapper.get(id);
        user.setDeptName(deptMapper.get(user.getDeptId()).getName());
        user.setroleIds(roleIds);
        return user;
    }

    @Override
    public ServerResponse list(int pageNum, int pageSize) {
        // startPage ----> start
        // 填充自己的sql查询逻辑
        // pageHelper---》收尾
        PageHelper.startPage(pageNum, pageSize);
        List<User> userList = userMapper.list();
        PageInfo pageInfo = new PageInfo(userList);
        return ServerResponse.createBySuccess(pageInfo);
    }

    @Override
    public int count(Map<String, Object> map) {
        return 0;
    }

    @Override
    public int save(User user) {
        int count = userMapper.save(user);
        Long userId = user.getUserId();
        List<Long> roles = user.getroleIds();
        userRoleMapper.removeByUserId(userId);
        List<UserRole> list = new ArrayList<>();
        for (Long roleId : roles) {
            UserRole ur = new UserRole();
            ur.setUserId(userId);
            ur.setRoleId(roleId);
            list.add(ur);
        }
        if (list.size() > 0) {
            userRoleMapper.batchSave(list);
        }
        return count;
    }

    @Override
    public int update(User user) {
        return 0;
    }

    @Override
    public int remove(Long userId) {
        return 0;
    }

    @Override
    public int batchremove(Long[] userIds) {
        return 0;
    }

    @Override
    public boolean exit(Map<String, Object> params) {
        return false;
    }

    @Override
    public Set<String> listRoles(Long userId) {
        return null;
    }

    @Override
    public int resetPwd(User user) {
        return 0;
    }

    @Override
    public Tree<Dept> getTree() {
        return null;
    }


}
